from django.test import TestCase

# Create your tests here.
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'auction_api.settings')
# 如果只是使用了logging日志功能的话可以不写以下两句，因为logging是python提供的模块，但是将来可能使用celery来执行其他的django任务，所以我们先写上
import django

django.setup()

import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.sms.v20210111 import sms_client, models
from django.conf import settings


def sms_msg(phone, tpl_id, code):
    try:
        phone = '+86%s' % phone
        cred = credential.Credential(settings.TENCENT_SECRET_ID, settings.TENCENT_SECRET_KEY)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "sms.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)

        req = models.SendSmsRequest()
        params = {
            "PhoneNumberSet": [phone, ],  # 手机号
            "SmsSdkAppId": settings.TENCENT_APP_ID,  # 应用AppId，v2要应用sdk/key，v3新增了密钥,取消了key
            "SignName": settings.TENCENT_SIGN,  # 签名的模版名称
            "TemplateId": tpl_id,  # 短信模板的id,str格式
            "TemplateParamSet": code  # 格式必须是 ["验证码",参数]  参数可以为空，具体看自己的模板要求
        }
        req.from_json_string(json.dumps(params))

        resp = client.SendSms(req)
        print(resp.to_json_string())

    except TencentCloudSDKException as err:
        print(err)


import random

ret1 = random.randint(1000, 9999)



ret2 = str(random.randrange(1000, 9999))
print(ret1)
sms_msg('15538987321', '993155', [ret1])
